package com.hunter.tools;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.database.bean.WebsitsJob;

public class ReadSitesFromXML {
	private String xmlpath;
	private File xml;
	public ReadSitesFromXML()
	{
		this.xmlpath=new File(ReadSitesFromXML.class.getClassLoader().getResource("\\").getPath()).getParent()+ File.separator+"sites.xml";
		this.xml=new File(xmlpath);
	}
	public List<WebsitsJob> getWebsites()
	{
		List<WebsitsJob> websites=new ArrayList<WebsitsJob>();
		try {
			Document doc = Jsoup.parse(xml, "utf8");
			Elements sites=doc.getElementsByTag("site");
			for(Element site:sites)
			{
				WebsitsJob websitesJobt=new WebsitsJob();
				websitesJobt.setId(site.attr("id"));
				websitesJobt.setName(site.attr("name")==null?"":site.attr("name"));
				websitesJobt.setUrl(site.attr("url")==null?"":site.attr("url"));
				websitesJobt.setList_anchor(site.attr("list_anchor")==null?"":site.attr("list_anchor"));
				websitesJobt.setTitletag(site.attr("titletag")==null?"":site.attr("titletag"));
				websitesJobt.setContenttag(site.attr("contenttag")==null?"":site.attr("contenttag"));
				websitesJobt.setTask_cron(site.attr("task_cron")==null?"":site.attr("task_cron"));
				websitesJobt.setContent_encode(site.attr("content_encode")==null?"utf8":site.attr("content_encode"));
				websites.add(websitesJobt);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return websites;
	}
}
